<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends NavController {

    public function ajaxGetMemberPrice(){
        $goodsId = I('get.goods_id');
        $gModel = D('Admin/Goods');
        echo ($gModel->getMemberPrice($goodsId));
    }

    public function index(){
        $goodModel = D('Admin/Goods');
        $goods1 = $goodModel->getPromoteGoods();
        $goods2 = $goodModel->getRecGoods('is_new');
        $goods3 = $goodModel->getRecGoods('is_hot');
        $goods4 = $goodModel->getRecGoods('is_best');

        $catModel= D('Admin/Category');
        $floorData = $catModel->floorData();//print_r($floorData);exit;

        $this->assign(array(
           'goods1' => $goods1,
           'goods2' => $goods2,
           'goods3' => $goods3,
           'goods4' => $goods4,
            'floorData' => $floorData,
        ));
        $this->assign(array(
            '_show_nav' => 1,
            '_page_title' => '首页',
            '_page_keywords' => '首页',
            '_page_description' => '首页',
        ));
        $this->display();
    }

    public function goods() {
    // 接收商品的ID
        $id = I('get.id');
        // 根据ID取出商品的详细信息
        $gModel = D('Goods');
        $info = $gModel->where('id='.$id)->find();//var_dump($info);exit;
        // 再根据主分类ID找出这个分类所有上级分类制作导航
        $catModel = D('Admin/Category');
        $catPath = $catModel->parentPath($info['cat_id']);
        // 取出商品的相册
        $gpModel = D('goods_pic');
        $gpData = $gpModel->where(array(
            'goods_id' => array('eq', $id),
        ))->select();
        //header('Content-Type:text/html;charset=utf-8;');
        // 取出这件商品所有的属性
        $gaModel = D('goods_attr');
        $gaData = $gaModel->alias('a')
            ->field('a.*,b.attr_name,b.attr_type')
            ->join('LEFT JOIN __ATTRIBUTE__ b ON a.attr_id=b.id')
            ->where(array(
                'a.goods_id' => array('eq', $id),
            ))
            ->select();
        // 整理所有的商品，把唯一的和可选的属性分开存放
        $uniArr = array();  // 唯一属性
        $mulArr = array();  // 可选属性
        foreach ($gaData as $k => $v)
        {
            if($v['attr_type'] == '唯一')
                $uniArr[] = $v;
            else
                // 把同一个属性放到一起 -》 三维
                $mulArr[$v['attr_name']][] = $v;
        }
        // 取出这件商品所有的会员价格
        $mpModel = D('member_price');
        $mpData = $mpModel->alias('a')
            ->field('a.price,b.level_name')
            ->join('LEFT JOIN __MEMBER_LEVEL__ b ON a.level_id=b.id')
            ->where(array(
                'a.goods_id' => array('eq', $id),
            ))->select();

        $viewPath = C('IMAGE_CONFIG');

        $this->assign(array(
            'info' => $info,
            'catPath' => $catPath,
            'gpData' => $gpData,
            'uniArr' => $uniArr,
            'mulArr' => $mulArr,
            'mpData' => $mpData,
            'viewPath' => $viewPath['viewPath'],
        ));

        // 设置页面信息
        $this->assign(array(
            '_show_nav' => 0,
            '_page_title' => '商品详情页',
            '_page_keywords' => '商品详情页',
            '_page_description' => '商品详情页',
        ));
        $this->display();
    }

    public function displayHistory(){
        $id = I('get.id');
        $data = isset($_COOKIE['display_history']) ? unserialize($_COOKIE['display_history']) : array(0);
        array_unshift($data,$id);
        $data = array_unique($data);
        if (count($data) > 6)
            $data = array_slice($data,0,6);
        setcookie('display_history',serialize($data),time() + 30*86400,'/');
        $goodsModel = D('Goods');
        //$data = implode(',',$data);
        var_dump($data);exit;
        $gData = $goodsModel->field('id,mid_logo,goods_name')->where(array(
            'id' => array('in',$data),
            'is_on_sale' => array('eq','是'),
        ))->order("FIELD(id,$data)")->select();//var_dump($gData);exit;
        echo json_encode($gData);
    }
}